package com.edunera;

import android.content.Context;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerTabStrip;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.MenuItem;
import com.edunera.connection.TaskGetCourseDetail;
import com.edunera.connection.TaskGetCourses;
import com.edunera.models.edu_courses;


public class ActivityDetail extends FragmentActivity {
    final Context context=this;
    private String user_device_id;

    ViewPager viewPager;
    PagerTabStrip tabStrip;
    private edu_courses course;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_course_detail);

        viewPager = (ViewPager) findViewById(R.id.viewPager);
        tabStrip = (PagerTabStrip) findViewById(R.id.tabStrip);
        int itemId = getIntent().getIntExtra("itemId", -1);
        user_device_id= Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID);

        if(itemId == -1) {
            Log.e("DETAIL", "invalid itemId");
        }
        getActionBar().setDisplayHomeAsUpEnabled(true);

        new TaskGetCourseDetail(context,user_device_id, String.valueOf(itemId)) {
            @Override
            public void onComplete() {
                if(edu_course!=null)   {
                    course=edu_course;
                    setTitle(course.name);

                    viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));

                }
            }
        }.execute();



    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);    //To change body of overridden methods use File | Settings | File Templates.
    }

    public class MyAdapter extends FragmentPagerAdapter {
        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return 3;
        }

        @Override
        public Fragment getItem(int position) {
            Fragment fragment;
            Bundle bundle = new Bundle();
            switch (position) {
                case 0:
                    fragment = new FragDetail();
                    bundle.putSerializable("details", course);
                    break;
                case 1:
                    fragment = new FragComment();
                    bundle.putSerializable("comments", course.comments);
                    break;
                case 2:
                    fragment = new FragTags();
                    bundle.putSerializable("tags", course.tags);
                    break;
                default:
                    fragment = null;

            }
            fragment.setArguments(bundle);
            return fragment;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return getString(R.string.tab_detail);
                case 1:
                    return getString(R.string.tab_comments);
                case 2:
                    return getString(R.string.tab_tags);
            }
            return "";
        }


    }

}
